﻿using System;
using System.Data.SqlClient;
using System.Reflection;
using System.Configuration;
using Zeus.Thunder.Core;

namespace SharpTrainer.DataAccess
{
  class ExecuteReaderDemo : ITestCase
  {
    public void Run()
    {
      // And this is the SQL statement that will be issued
      string select = "SELECT ContactName,CompanyName,Country FROM Customers";

      string source = ConfigurationSettings.AppSettings["connectionString"];
        
      try
      {
        // Connect to the database...
        using (SqlConnection conn = new SqlConnection(source))
        {
          // Open the database connection
          conn.Open();

          // Create the SQL command...
          SqlCommand cmd = new SqlCommand(select, conn);

          // Construct the data reader
          using (SqlDataReader reader = cmd.ExecuteReader())
          {
            Console.WriteLine("-----------------------------Customers Info-------------------------------------");

            // And iterate through the data
            while (reader.Read())
            {
              Console.WriteLine("Contact: {0}, Company: {1}, City: {2}", reader[0], reader[1], reader[2]);
            }

            reader.Close();
          }

          conn.Close();
        }
      }
      catch (Exception e)
      {
        Console.WriteLine(e.ToString());
      }

      Console.ReadLine();
    }
  }
}
